<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 计算最大乘积（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h3 id="main-toc">题目描述</h3> 
<p>给定一个元素类型为小写字符串的数组&#xff0c;请计算两个没有相同字符的元素长度乘积的最大值&#xff0c;</p> 
<p>如果没有符合条件的两个元素&#xff0c;返回0。</p> 
<p></p> 
<h3 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h3> 
<p>输入为一个半角逗号分隔的小写字符串的数组&#xff0c;2 &lt;&#61; 数组长度&lt;&#61;100&#xff0c;0 &lt; 字符串长度&lt;&#61; 50。</p> 
<p></p> 
<h3 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h3> 
<p>两个没有相同字符的元素 长度乘积的<strong>最大值</strong>。</p> 
<p></p> 
<h3 id="%E7%94%A8%E4%BE%8B">用例</h3> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">iwdvpbn,hk,iuop,iikd,kadgpf</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">14</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>数组中有5个元素。</p> <p>iwdvpbn与hk无相同的字符&#xff0c;满足条件&#xff0c;iwdvpbn的长度为7&#xff0c;hk的长度为2&#xff0c;乘积为14&#xff08;7*2&#xff09;。</p> <p>iwdvpbn与iuop、iikd、kadgpf均有相同的字符&#xff0c;不满足条件。</p> <p>iuop与iikd、kadgpf均有相同的字符&#xff0c;不满足条件。</p> <p>iikd与kadgpf有相同的字符&#xff0c;不满足条件。</p> <p>因此&#xff0c;输出为14。</p> </td></tr></tbody></table> 
<p></p> 
<h3>位运算解法</h3> 
<p>具体解析可以看&#xff1a;<a href="https://fcqian.blog.csdn.net/article/details/133788623?spm&#61;1001.2014.3001.5502" rel="nofollow" title="LeetCode - 318 最大单词长度乘积&#xff08;Java &amp; JS &amp; Py &amp; C&#xff09;-CSDN博客">LeetCode - 318 最大单词长度乘积&#xff08;Java &amp; JS &amp; Py &amp; C&#xff09;-CSDN博客</a></p> 
<p></p> 
<h4>JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  console.log(getResult(line.split(&#34;,&#34;)));
});

function getResult(words) {
  let ans &#61; 0;

  const n &#61; words.length;

  const bits &#61; new Array(n).fill(0);
  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    for (let j &#61; 0; j &lt; words[i].length; j&#43;&#43;) {
      bits[i] |&#61; 1 &lt;&lt; (words[i][j].charCodeAt() - 97);
    }
  }

  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    for (let j &#61; i &#43; 1; j &lt; n; j&#43;&#43;) {
      if ((bits[i] &amp; bits[j]) &#61;&#61; 0) {
        ans &#61; Math.max(ans, words[i].length * words[j].length);
      }
    }
  }

  return ans;
}
</code></pre> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    System.out.println(getResult(sc.nextLine().split(&#34;,&#34;)));
  }

  public static int getResult(String[] words) {
    int ans &#61; 0;

    int n &#61; words.length;
    int[] bits &#61; new int[n];

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; 0; j &lt; words[i].length(); j&#43;&#43;) {
        bits[i] |&#61; 1 &lt;&lt; (words[i].charAt(j) - &#39;a&#39;);
      }
    }

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; i &#43; 1; j &lt; n; j&#43;&#43;) {
        if ((bits[i] &amp; bits[j]) &#61;&#61; 0) {
          ans &#61; Math.max(ans, words[i].length() * words[j].length());
        }
      }
    }

    return ans;
  }
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
words &#61; input().split(&#34;,&#34;)


# 算法入口
def getResult():
    ans &#61; 0

    n &#61; len(words)

    bits &#61; [0] * n
    for i in range(n):
        for j in range(len(words[i])):
            bits[i] |&#61; (1 &lt;&lt; (ord(words[i][j]) - 97))

    for i in range(n):
        for j in range(i &#43; 1, n):
            if (bits[i] &amp; bits[j]) &#61;&#61; 0:
                ans &#61; max(ans, len(words[i]) * len(words[j]))

    return ans


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#define MAX(a,b) (a) &gt; (b) ? (a) : (b)

int getResult(char ** words, int wordsSize);

int main() {
    char s[5000];
    gets(s);

    char* words[100];
    int wordsSize &#61; 0;

    char* token &#61; strtok(s, &#34;,&#34;);
    while(token !&#61; NULL) {
        words[wordsSize&#43;&#43;] &#61; token;
        token &#61; strtok(NULL, &#34;,&#34;);
    }

    printf(&#34;%d\n&#34;, getResult(words, wordsSize));

    return 0;
}

int getResult(char ** words, int wordsSize) {
    int ans &#61; 0;

    int* bits &#61; (int*) calloc(wordsSize, sizeof(int));

    for(int i&#61;0; i&lt;wordsSize; i&#43;&#43;) {
        for(int j&#61;0; j&lt;strlen(words[i]); j&#43;&#43;) {
            bits[i] |&#61; 1 &lt;&lt; (words[i][j] - &#39;a&#39;);
        }
    }

    for(int i&#61;0; i&lt;wordsSize; i&#43;&#43;) {
        for(int j&#61;i&#43;1; j&lt;wordsSize; j&#43;&#43;) {
            if((bits[i] &amp; bits[j]) &#61;&#61; 0) {
                ans &#61; MAX(ans, strlen(words[i]) * strlen(words[j]));
            }
        }
    }

    return ans;
}</code></pre> 
<p></p> 
<h3 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">交集解法</h3> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34;,&#34;);
  console.log(getResult(arr));
});

function getResult(arr) {
  let ans &#61; 0;

  const sets &#61; arr.map((s) &#61;&gt; new Set([...s]));

  for (let i &#61; 0; i &lt; sets.length; i&#43;&#43;) {
    for (let j &#61; i &#43; 1; j &lt; sets.length; j&#43;&#43;) {
      if (disjoint(sets[i], sets[j])) {
        ans &#61; Math.max(ans, arr[i].length * arr[j].length);
      }
    }
  }

  return ans;
}

function disjoint(set1, set2) {
  for (let c of set1) {
    if (set2.has(c)) return false;
  }
  return true;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String[] strings &#61; sc.nextLine().split(&#34;,&#34;);
    System.out.println(getResult(strings));
  }

  public static int getResult(String[] arr) {
    ArrayList&lt;HashSet&lt;Character&gt;&gt; list &#61; new ArrayList&lt;&gt;();

    for (String s : arr) {
      HashSet&lt;Character&gt; set &#61; new HashSet&lt;&gt;();
      for (char c : s.toCharArray()) set.add(c);
      list.add(set);
    }

    int ans &#61; 0;

    for (int i &#61; 0; i &lt; list.size(); i&#43;&#43;) {
      HashSet&lt;Character&gt; a &#61; list.get(i);
      for (int j &#61; i &#43; 1; j &lt; list.size(); j&#43;&#43;) {
        HashSet&lt;Character&gt; b &#61; list.get(j);
        if (Collections.disjoint(a, b)) {
          ans &#61; Math.max(ans, arr[i].length() * arr[j].length());
        }
      }
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split(&#34;,&#34;)


# 算法入口
def getResult():
    sets &#61; list(map(lambda x: set(x), arr))

    ans &#61; 0

    for i in range(len(sets)):
        for j in range(i&#43;1, len(sets)):
            if sets[i].isdisjoint(sets[j]):
                ans &#61; max(ans, len(arr[i]) * len(arr[j]))

    return ans


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>